package com.mingqijia.gassafety.job.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.db.entity.ConsumerLocation;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.entity.vo.ConsumerLocationStatItemVO;
import com.mingqijia.gassafety.db.mapper.ConsumerLocationMapper;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.job.service.ConsumerLocationService;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 客户地图
 * @Author: duanhongxiang
 * @Date: 2024/4/26 15:23
 */
@Service
@Slf4j
public class ConsumerLocationServiceImpl extends ServiceImpl<ConsumerLocationMapper, ConsumerLocation> implements ConsumerLocationService {
    @Resource
    ConsumerLocationMapper consumerLocationMapper;
    
    @Resource
    DtuMapper dtuMapper;

    @Override
    public void updateConsumerLocationDeviceNum(String spId, Integer consumerId) {
        try {
            log.info("updateConsumerLocationDeviceNum>>更新t_consumer_location表设备各个状态信息，spId:{},consumerId:{}", spId, consumerId);
            ConsumerLocationStatItemVO consumerLocationStatItemVO = consumerLocationMapper.selectConsumerLocationStat(spId, consumerId);
            log.info("consumerLocationStatItemVO:{}", consumerLocationStatItemVO);
            if (ObjectUtil.isEmpty(consumerLocationStatItemVO)) {
                consumerLocationStatItemVO = new ConsumerLocationStatItemVO();
                consumerLocationStatItemVO.setConsumerId(consumerId);
                consumerLocationStatItemVO.setDeviceNum(0);
                consumerLocationStatItemVO.setAlertNum(0);
                consumerLocationStatItemVO.setBrokenNum(0);
                consumerLocationStatItemVO.setOfflineNum(0);
            }
            log.info("更新t_consumer_location表设备各个状态信息，spId:{},consumerId:{},consumerLocationStatItemVO:{}",
                    spId, consumerId, JSONObject.toJSONString(consumerLocationStatItemVO));
            consumerLocationMapper.updateConsumerDeviceNum(spId, consumerId, consumerLocationStatItemVO);
        }catch (Exception e){
            log.error("同步更新t_consumer_location表失败",e);
        }
    }

    @Override
    public void updateConsumerLocationDeviceNumByDeviceId(String spId, String deviceId) {
        try {
            log.info("更新t_consumer_location表设备各个状态信息，spId:{},deviceId:{}", spId, deviceId);
            Dtu dtu = dtuMapper.selectById(deviceId);
            if (ObjectUtil.isEmpty(dtu) || null == dtu.getConsumerId()) {
                return;
            }
            updateConsumerLocationDeviceNum(spId, dtu.getConsumerId());
        }catch(Exception e){
            log.error("同步更新t_consumer_location表失败",e);
        }
    }

    @Override
    public void updateConsumerLocationDeviceNumBySubsCode(String subsCode) {
        try {
            log.info("更新t_consumer_location表设备各个状态信息，subsCode:{}", subsCode);
            if(!StringUtils.isEmpty(subsCode)){
                consumerLocationMapper.updateConsumerLocationBySubsCode(subsCode);
            }
        }catch(Exception e){
            log.error("同步更新t_consumer_location表失败",e);
        }
    }
}
